import React from 'react';
import * as mobx from 'mobx';
import {WeaDialog,WeaAuth,WeaRightMenu,WeaLocaleProvider} from 'ecCom';
import {Button,Modal} from 'antd';
import WorkflowShare from '../share/WorkflowShare';
import { inject, observer} from 'mobx-react';

const getLabel = WeaLocaleProvider.getLabel;

@inject ('wfReportShareStore')

@observer

export default class Main extends React.Component{
    constructor(props){
        super(props);
    }

    getSelectedRowKeysTail(){
        const {wfReportShareStore} = this.props;
        const {tableStore} = wfReportShareStore;
        const tableNow = tableStore;
        const selectedRowKeys = tableNow.selectedRowKeys;
        const idsJson = JSON.stringify(selectedRowKeys);
        let ids = idsJson.slice(1,idsJson.length-1).replace(/\"/g,"");
        return ids;
    }

    addShare () {
        const {wfReportShareStore } = this.props;
        wfReportShareStore.toogleAddShare(true);
    }

    addShareOk (v) {
        const {wfReportShareStore } = this.props;
        let {sharetype,sharedobj,sharelevel,seclevel,seclevelmax} = v;

        if(!seclevel){
            seclevel = 0;
        }
        if(!seclevelmax){
            if(Number(seclevelmax) === 0){
                seclevelmax = 0;
            }else {
                seclevelmax = 255;
            }
        }

        let params = {
            sharetype,
            sharedobj,
            sharelevel,
            seclevel,
            seclevelmax
        };
        wfReportShareStore.toogleAddShare(false);
        wfReportShareStore.addShare(params);
    }

    deleteShare (ids) {
        Modal.confirm({
            title: getLabel(131329,'信息确认'),
            content: getLabel(129890,'确定要删除吗？'),
            okText: getLabel(83446,'确定'),
            cancelText: getLabel(31129,'取消'),
            onOk: ()=>{
                const {wfReportShareStore } = this.props;
                wfReportShareStore.deleteShare({shareids:ids})
            }
        });
    }

    handleCancel () {
        const {wfReportShareStore } = this.props;
        wfReportShareStore.toogleAddShare(false);
    }

    shareModalCancel  = ()  =>{
        const {wfReportShareStore } = this.props;
        wfReportShareStore.toogleShareModal(false);
    }

    operates (record,index,operate,flag) {
        this.deleteShare(record.id)
    }

    getMoreBtns () {
        const {wfReportShareStore } = this.props;
        let { reportname } = wfReportShareStore;
        let btns = {
            datas: [],
            collectParams: {
                favname: `${getLabel(119,'共享')}:${reportname}`,
                favouritetype: 2,
                objid: 0,
                importantlevel: 1,
            }
        };

        return btns;
    }
    shareCancel = () => {this.props.wfReportShareStore.toogleAddShare(false)};
    render () {

        const {wfReportShareStore } = this.props;
        let { tableStore,shareModalVisible,addShareVisible,conditions,reportname } = wfReportShareStore;
        conditions = mobx.toJS(conditions);
        const footer = [
            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@3uxy0j@shareModalCancel`} key="cancel" type="ghost" onClick={()=>this.shareModalCancel()}>{getLabel(31129,'取消')}</Button>
        ];
        const ids = this.getSelectedRowKeysTail();
        return (
            <div>
                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@a3vkjv`}
                    className="wf-shareModal"
                    title={`${getLabel(119,'共享')} : ${reportname}`}
                    visible={shareModalVisible}
                    icon={"icon-coms-workflow"}
                    iconBgcolor="#0079DE"
                    maskClosable={false}
                    // buttons={[]}
                    onCancel={this.shareModalCancel}
                    style={{width: '800px', height: '490px'}}
                    moreBtn={this.getMoreBtns()}
                >
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@fo38h6`}
                        {...this.getMoreBtns()}
                    >
                        <WorkflowShare ecId={`${this && this.props && this.props.ecId || ''}_WorkflowShare@s43v3t`}
                            onOperatesClick={(record,index,operate,flag)=>{this.operates(record,index,operate,flag)}}
                            title={getLabel(383654,'共享列表')}
                            jurisdiction={true}
                            tableStore={tableStore}
                            deleteConfirm={(ids)=>this.deleteShare(ids)}
                            addShare={()=>this.addShare()}
                            ids={() => ids}
                        />
                    </WeaRightMenu>
                    <WeaAuth ecId={`${this && this.props && this.props.ecId || ''}_WeaAuth@qdjlyg`}
                        visible={addShareVisible}
                        conditions={conditions}
                        icon="icon-coms-workflow"
                        iconBgcolor='#0079DE'
                        title={getLabel(383655,'添加共享')}
                        onOk={v => this.addShareOk(v)}
                        onCancel={this.shareCancel}
                    />
                </WeaDialog>
            </div>
        )
    }

}